package com.lc.afbx.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lc.afbx.BaseApplication;
import com.lc.afbx.R;
import com.lc.afbx.conn.PostApiAddAdderss;
import com.lc.afbx.util.MEditTextListener;
import com.lc.afbx.util.MyToast;
import com.lc.afbx.util.PrefrenceConfig;
import com.lc.afbx.util.Validator;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilToast;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import cn.trinea.android.common.util.ToastUtils;

/**
 * Created by Administrator on 2016/6/2.
 */
public class AddAddressActivity extends BaseActivity implements View.OnClickListener {


    private EditText address_add_name_et;
    private TextView address_add_area_tv;
    private EditText address_add_phone_et;
    private EditText address_add_detail_address_et;

    private ImageView address_add_isfirst_iv;
    private boolean isFirst = true;


    private PostApiAddAdderss postApiAddAdderss = new PostApiAddAdderss(new AsyCallBack<PostApiAddAdderss.Info>() {

        @Override
        public void onSuccess(String toast, int type, PostApiAddAdderss.Info info) throws Exception {

            MyToast.showToast("添加成功", AddAddressActivity.this);

            AddressActivity.RefreshAddressCallback refreshAddressCallback = (AddressActivity.RefreshAddressCallback) getAppCallBack(AddressActivity.class);
            refreshAddressCallback.refresh();

            finish();

        }

        @Override
        public void onFail(String toast, int type) throws Exception {

            UtilToast.show(AddAddressActivity.this, toast);
        }
    });


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_geren_addaddress);

        address_add_name_et = ((EditText) findViewById(R.id.address_add_name_et));
        address_add_area_tv = ((TextView) findViewById(R.id.address_add_area_tv));
        address_add_phone_et = ((EditText) findViewById(R.id.address_add_phone_et));
        address_add_detail_address_et = ((EditText) findViewById(R.id.address_add_detail_address_et));

        address_add_isfirst_iv = ((ImageView) findViewById(R.id.address_add_isfirst_iv));

        address_add_name_et.addTextChangedListener(new MEditTextListener(context, 16, address_add_name_et));
        address_add_phone_et.addTextChangedListener(new MEditTextListener(context, 11, address_add_phone_et));
        address_add_detail_address_et.addTextChangedListener(new MEditTextListener(context, 30, address_add_detail_address_et));

    }


    @Override
    public void onClick(View view) {


        switch (view.getId()) {

            case R.id.back:

                finish();
                return;

            case R.id.address_add_save_bt:

                String nameStr = address_add_name_et.getText().toString().trim().replaceAll(" ","");
                String phoneStr = address_add_phone_et.getText().toString().trim().replaceAll(" ","");
                String addressStr = address_add_detail_address_et.getText().toString().trim().replaceAll(" ", "");

                if (TextUtils.isEmpty(nameStr)) {
                    MyToast.showToast("收件人姓名不能为空", context);
                    return;
                }
                if (TextUtils.isEmpty(phoneStr)) {
                    MyToast.showToast("手机号码不能为空", context);
                    return;
                }
                if (!Validator.isMobile(phoneStr)) {
                    MyToast.showToast("手机号码格式不正确", context);
                    return;
                }
                if (TextUtils.isEmpty(address_add_area_tv.getText().toString())) {
                    MyToast.showToast("所在地区不能为空", context);
                    return;
                }
                if (TextUtils.isEmpty(addressStr)) {
                    MyToast.showToast("详细地址不能为空", context);
                    return;
                }

                postApiAddAdderss.is_first = String.valueOf(isFirst);
                postApiAddAdderss.rec_name = nameStr;
                postApiAddAdderss.phone = phoneStr;
                postApiAddAdderss.cur_address = address_add_area_tv.getText().toString();
                postApiAddAdderss.road_address = addressStr;
                postApiAddAdderss.userid = BaseApplication.BasePreferences.readUserId();
                postApiAddAdderss.execute(this);


                return;

            case R.id.address_add_area_rl:

                startActivityForResult(new Intent(this, ChoseAreaActivity.class).putExtra(PrefrenceConfig.INTENT_TAG, ""), 3);

                return;

            case R.id.address_add_isfirst_rl:

                if (!isFirst) {

                    isFirst = true;
                    address_add_isfirst_iv.setImageResource(R.mipmap.szmrdz);
                    MyToast.showToast("设为默认地址", AddAddressActivity.this);

                } else {

                    isFirst = false;
                    address_add_isfirst_iv.setImageResource(R.mipmap.lanquan);
                    MyToast.showToast("不设为默认地址", AddAddressActivity.this);

                }

                return;


        }


    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

//        UtilLog.e("看看","这个方法走没走啊");
        switch (resultCode) {
            case 3:

                Bundle bundle = data.getExtras();

                ArrayList list = bundle.getParcelableArrayList("list");

                List<Map<String, String>> lists = (List<Map<String, String>>) list.get(0);

                StringBuffer sb = new StringBuffer();

                for (int i = 0; i < lists.size(); i++) {

                    sb.append(lists.get(i).get("title"));

                }

                address_add_area_tv.setText(sb.toString());

                break;
            default:
                break;
        }
    }

}
